{extend name="Base" /}
{block name="title"}{$title}{/block}
{block name="body"}
<style>
	.layui-inline {
		min-width: 500px;
	}
</style>
<fieldset class="layui-elem-field layui-field-title">
	<legend>{$name}</legend>
</fieldset>

<div class="toolbar">
	<div class="layui-inline">
		<input type="text" name="search" placeholder="请输入检索依据，可以是UID，用户名，邮箱，注册IP或UUID" autocomplete="off" class="layui-input">
	</div>
	<button class="layui-btn search" data-type="reload">检索</button>
	<button class="layui-btn alldel" data-type="getCheckData">批量删除</button>
</div>

<table id="users" lay-filter="users" class="layui-hide"></table>

<script type="text/html" id="bar">
	<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
	<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script type="text/html" id="softdel">
	{{#  if(d.softdel === 1){ }}
	<div style="color: #FF0000">{{d.ssoftdel}}</div>
	{{#  } else { }}
	<div>{{d.ssoftdel}}</div>
	{{#  } }}
</script>

<script>
	layui.use(['layer', 'table'], function () {
		var layer = layui.layer;
		var table = layui.table;

		table.render({
			id: "search",
			elem: '#users',
			page: true,
			height: 685,
			even: true,
			method: 'POST',
			url: "/backend/users/doSearch",
			where: {type: 1},
			cols: [[
				{checkbox: true, fixed: true},
				{field: 'uid', title: 'UID', width: 60, fixed: true},
				{field: 'username', title: '用户名', sort: true, width: 120, fixed: true},
				{field: 'ssoftdel', title: '软删除', sort: true, width: 80, event: 'ssoftdel', style: 'cursor: pointer;', fixed: true, templet: '#softdel'},
				{field: 'email', title: '邮箱', sort: true, width: 80, event: 'email', style: 'cursor: pointer;'},
				{field: 'sban', title: '封禁', sort: true, width: 80, event: 'sban', style: 'cursor: pointer;'},
				{field: 'class', title: '等级', sort: true, width: 80},
				{field: 'price', title: '积分', sort: true, width: 80, event: 'price', style: 'cursor: pointer;'},
				{field: 'hr', title: 'H&R', sort: true, width: 80, event: 'hr', style: 'cursor: pointer;'},
				{field: 's2fa', title: '2FA', sort: true, width: 80, event: 's2fa', style: 'cursor: pointer;'},
				{field: 'invitebyname', title: '邀请人', sort: true, width: 80},
				{field: 'addtime', title: '注册时间', sort: true, width: 120},
				{field: 'ip', title: '注册IP', sort: true, width: 80},
				{field: 'lasttime', title: '最后访问', sort: true, width: 120},
				{field: 'nuploaded', title: '上传量', sort: true, width: 120, event: 'nuploaded', style: 'cursor: pointer;'},
				{field: 'ndownloaded', title: '下载量', sort: true, width: 120, event: 'ndownloaded', style: 'cursor: pointer;'},
				{field: 'seedtime', title: '总做种时间', sort: true, width: 150},
				{field: 'leechtime', title: '总下载时间', sort: true, width: 150},
				{field: 'seed', title: '做种数', sort: true, width: 80},
				{field: 'leech', title: '下载数', sort: true, width: 80},
				{field: 'complete', title: '完成数', sort: true, width: 80},
				{field: 'longinv', title: '永久邀请', sort: true, width: 120, event: 'longinv', style: 'cursor: pointer;'},
				{field: 'shortinv', title: '临时邀请', sort: true, width: 120, event: 'shortinv', style: 'cursor: pointer;'},
				{field: 'sseal', title: '挂起状态', sort: true, width: 120, event: 'sseal', style: 'cursor: pointer;'},
				{field: 'ssealtime', title: '挂起时间', sort: true, width: 120},

				{title: '操作', width: 150, align: 'center', toolbar: '#bar', fixed: 'right'}
			]]
		});

		var active = {
			getCheckData: function () {
				var checkStatus = table.checkStatus('search'), data = checkStatus.data;
				layer.confirm('该删除为彻底删除，你确定要彻底删除么？', function () {
					$.ajax({
						type: "POST",
						url: "/backend/users/delUsers",
						cache: false,
						data: {
							array: data,
							type: 1
						},
						dataType: "json",
						success: function (e) {
							if (e.status === 1) {

								table.reload('search', {
									where: {
										search: ""
									}
								});
								layer.alert(e.message, {
									//成功图标
									icon: 1
								});

							} else if (e.status === 0) {
								layer.alert(e.message, {
									//失败图标
									icon: 2
								});
							} else {
								layer.alert("系统错误，请稍后重试", {
									//失败图标
									icon: 2
								});
							}
						}
					});
				});
			},
			reload: function () {
				var $search = $("input[name=search]");
				table.reload('search', {
					where: {
						search: $search.val()
					}
				});
				$search.val("");
			}
		};

		$('.toolbar .alldel').on('click', function () {
			var type = $(this).data('type');
			active[type] ? active[type].call(this) : '';
		});

		$('.toolbar .search').on('click', function () {
			var type = $(this).data('type');
			active[type] ? active[type].call(this) : '';
		});

		//监听工具条
		table.on('tool(users)', function (obj) {
			var data = obj.data;
			if (obj.event === 'detail') {
				layer.open({
					type: 1,
					title: false,
					closeBtn: false,
					shadeClose: true,
					area: '300px;',
					shade: 0.8,
					id: 'alert',
					btn: '关闭',
					moveType: 1,
					content: '<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">' +
					"UID：" + data.uid + "<br/>" +
					"用户名：" + data.username + "<br/>" +
					"软删除：" + data.ssoftdel + "<br/>" +
					"邮箱：" + data.email + "<br/>" +
					"封禁：" + data.sban + "<br/>" +
					"等级：" + data.class + "<br/>" +
					"积分：" + data.price + "<br/>" +
					"H&R：" + data.hr + "<br/>" +
					"2FA：" + data.s2fa + "<br/>" +
					"邀请人：" + data.invitebyname + "<br/>" +
					"注册时间：" + data.addtime + "<br/>" +
					"注册IP：" + data.ip + "<br/>" +
					"最后访问：" + data.lasttime + "<br/>" +
					"上传量：" + data.nuploaded + "<br/>" +
					"下载量：" + data.ndownloaded + "<br/>" +
					"总做种时间：" + data.seedtime + "<br/>" +
					"总下载时间：" + data.leechtime + "<br/>" +
					"做种数：" + data.seed + "<br/>" +
					"下载数：" + data.leech + "<br/>" +
					"完成数：" + data.complete + "<br/>" +
					"挂起状态：" + data.sseal + "<br/>" +
					"挂起时间：" + data.ssealtime + "<br/>" +
					'</div>'
				});
			} else if (obj.event === 'del') {
				layer.confirm('该删除为彻底删除，你确定要彻底删除么？', function (index) {
					obj.del();
					layer.close(index);
					$.ajax({
						type: "POST",
						url: "/backend/users/delUsers",
						cache: false,
						data: {
							array: data,
							type: 1
						},
						dataType: "json",
						success: function (e) {
							if (e.status === 1) {
								layer.alert(e.message, {
									//成功图标
									icon: 1
								});
							} else if (e.status === 0) {
								layer.alert(e.message, {
									//失败图标
									icon: 2
								});
							} else {
								layer.alert("系统错误，请稍后重试", {
									//失败图标
									icon: 2
								});
							}
						}
					})
				});
			} else if (obj.event === 'email') {
				layer.prompt({
						title: '修改邮箱',
						value: data.email
					},
					function (value, index) {
						layer.close(index);

						ajax_send(data, "email", value);

						obj.update({
							email: value
						});
					}
				);
			} else if (obj.event === 'ssoftdel') {
				layer.prompt({
						title: '修改软删除状态，输入0或1，0=否，1=是',
						value: data.softdel === 0 ? "0" : data.softdel
					},
					function (value, index) {
						layer.close(index);

						ajax_send(data, "softdel", value);

						var v = "否";
						if (value === "1") {
							v = "是";
						}
						obj.update({
							softdel: value,
							ssoftdel: v
						});
					}
				);
			} else if (obj.event === 'sban') {
				layer.prompt({
						title: '修改封禁状态，输入0或1，0=否，1=是',
						value: data.ban === 0 ? "0" : data.ban
					},
					function (value, index) {
						layer.close(index);

						ajax_send(data, "ban", value);

						var v = "否";
						if (value === "1") {
							v = "是";
						}
						obj.update({
							ban: value,
							sban: v
						});
					}
				);
			} else if (obj.event === 'price') {
				layer.prompt({
						title: '修改积分',
						value: data.price === 0 ? "0" : data.price
					},
					function (value, index) {
						layer.close(index);

						ajax_send(data, "price", value);

						obj.update({
							price: value
						});
					}
				);
			} else if (obj.event === 'hr') {
				layer.prompt({
						title: '修改H&R',
						value: data.hr === 0 ? "0" : data.hr
					},
					function (value, index) {
						layer.close(index);

						ajax_send(data, "hr", value);

						obj.update({
							hr: value
						});
					}
				);
			} else if (obj.event === 's2fa') {
				layer.prompt({
						title: '修改2FA，输入0或1，0=否，1=是',
						value: data.n2fa === 0 ? "0" : data.n2fa
					},
					function (value, index) {
						layer.close(index);

						ajax_send(data, "2fa", value);

						var v = "否";
						if (value === "1") {
							v = "是";
						}
						obj.update({
							n2fa: value,
							s2fa: v
						});
					}
				);
			} else if (obj.event === 'nuploaded') {
				layer.prompt({
						title: '修改上传量',
						value: data.uploaded === 0 ? "0" : data.uploaded
					},
					function (value, index) {
						layer.close(index);

						ajax_send(data, "uploaded", value);

						obj.update({
							uploaded: value,
							nuploaded: getSize(value)
						});
					}
				);
			} else if (obj.event === 'ndownloaded') {
				layer.prompt({
						title: '修改下载量',
						value: data.downloaded === 0 ? "0" : data.downloaded
					},
					function (value, index) {
						layer.close(index);

						ajax_send(data, "downloaded", value);

						obj.update({
							downloaded: value,
							ndownloaded: getSize(value)
						});
					}
				);
			} else if (obj.event === 'longinv') {
				layer.prompt({
						title: '修改永久邀请',
						value: data.longinv === 0 ? "0" : data.longinv
					},
					function (value, index) {
						layer.close(index);

						ajax_send(data, "longinv", value);

						obj.update({
							longinv: value
						});
					}
				);
			} else if (obj.event === 'shortinv') {
				layer.prompt({
						title: '修改临时邀请',
						value: data.shortinv === 0 ? "0" : data.shortinv
					},
					function (value, index) {
						layer.close(index);

						ajax_send(data, "shortinv", value);

						obj.update({
							shortinv: value
						});
					}
				);
			} else if (obj.event === 'sseal') {
				layer.prompt({
						title: '修改挂起状态，输入0或1，0=否，1=是',
						value: data.seal === 0 ? "0" : data.seal
					},
					function (value, index) {
						layer.close(index);

						ajax_send(data, "seal", value);

						var v = "否";
						if (value === "1") {
							v = "是";
						}
						obj.update({
							seal: value,
							sseal: v
						});
					}
				);
			}
		});
	});

	function ajax_send(data, name, value) {
		$.ajax({
			type: "POST",
			url: "/backend/users/editUser",
			cache: false,
			data: {
				uid: data.uid,
				k: name,
				v: value
			},
			dataType: "json",
			success: function (e) {
				if (e.status === 1) {
					layer.alert(e.message, {
						//成功图标
						icon: 1
					});
				} else if (e.status === 0) {
					layer.alert(e.message, {
						//失败图标
						icon: 2
					});
				} else {
					layer.alert("系统错误，请稍后重试", {
						//失败图标
						icon: 2
					});
				}
			}
		});
	}

	function getSize(Byte) {
		var SizeByte = Byte;
		var SizeMsg = "";
		if (SizeByte < 1024) SizeMsg = (SizeByte) + " Byte";
		else if (SizeByte < 1048576) SizeMsg = (SizeByte / 1024) + " KB";
		else if (SizeByte > 1048576 && SizeByte < 1073741824) SizeMsg = (SizeByte / (1024 * 1024)) + " MB";
		else if (SizeByte > 1073741824 && SizeByte < 1099511627776) SizeMsg = (SizeByte / (1024 * 1024 * 1024)) + " GB";
		else if (SizeByte > 1099511627776 && SizeByte < 1125899906842624) SizeMsg = (SizeByte / (1024 * 1024 * 1024 * 1024)) + " PB";
		else if (SizeByte > 1125899906842624 && SizeByte < 1152921504606846976) SizeMsg = (SizeByte / (1024 * 1024 * 1024 * 1024 * 1024)) + " EB";
		return SizeMsg;
	}
</script>
{/block}